#!/bin/sh

############################################################
# NOTE: Remove this from the repo when 15.0 ships!         #
# Also remove cmake-202x from REQUIRES in all .info files! #
############################################################

# Slackware build script for cmake-202x

# Written by B. Watson (yalhcru@gmail.com)

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# This build exists because the cmake in Slackware 14.2 is too old,
# and can't be upgraded. This installs cmake under /opt, where it
# won't conflict with anything. README explains how to use it in your
# SlackBuild.

# cmake.manpages.tar.lz comes from Slackware, courtesy of Pat V. Since
# it's from -current (therefore subject to change), I mirrored the
# version I'm using on slackware.uk.

# 20210306 bkw: updated for 3.19.6 (sync with -current).
# 20201108 bkw: updated for 3.18.4, put version number in manpages URL.

PRGNAM=cmake-202x
SRCNAM=cmake
VERSION=${VERSION:-3.19.6}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $SRCNAM-$VERSION
tar xvf $CWD/$SRCNAM-$VERSION.tar.gz
cd $SRCNAM-$VERSION
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

PREFIX=${PREFIX:-/opt/$PRGNAM}
PKGPREFIX=$PKG/$PREFIX

# The --system-* and --no-system-* stuff is intended to use system
# versions of libraries that ship with core Slackware, and bundled
# (non-system) libs for everything that isn't in core, *even* if
# it's available on SBo. This will prevent SBo upgrades from randomly
# breaking things.

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./bootstrap \
  --system-curl \
  --system-expat \
  --no-system-jsoncpp \
  --system-zlib \
  --system-bzip2 \
  --system-liblzma \
  --no-system-nghttp2 \
  --no-system-zstd \
  --system-libarchive \
  --no-system-librhash \
  --no-system-libuv \
  --prefix=$PREFIX

make
make install/strip DESTDIR=$PKG

# To avoid a dependency on Sphinx (which has *26 dependencies* of its own),
# use Pat's prebuilt man pages.
MAN=$CWD/$SRCNAM.manpages.tar.lz
MANV=$CWD/$SRCNAM.manpages.$VERSION.tar.lz
[ -e "$MANV" ] && MAN="$MANV"

tar xvf $MAN
mv usr/man $PKGPREFIX/man
gzip -9 $PKGPREFIX/man/man?/*.?

# Nothing in the doc dir but licenses. Move it to where users expect
# to find it.
mkdir -p $PKG/usr/doc
mv $PKGPREFIX/doc/$SRCNAM-* $PKG/usr/doc/$PRGNAM-$VERSION
rm -rf $PKGPREFIX/doc

cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild
cat $CWD/README_SBo.txt > $PKG/usr/doc/$PRGNAM-$VERSION/README_SBo.txt

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-tgz}
